//
//  UIButton+JMImagePosition.m
//  Demo_ButtonImageTitleEdgeInsets
//
//  Created by luxiaoming on 16/1/15.
//  Copyright © 2016年 luxiaoming. All rights reserved.
//

/**
 *  //  https://blog.csdn.net/gaoyp/article/details/51958274
 *  button的imageView最终水平位置为：imageView.frame.origin.x = imageView.frame.origin.x + imageEdgeInsets.left/2.0 - imageEdgeInsets.right/2.0。
 *  结果就是：
 *  imageEdgeInsets的left如果为+10,则button的imageView向右移动5。
 *  imageEdgeInsets的left如果为-10,则button的imageView向左移动5。
 *  imageEdgeInsets的right如果为+10,则button的imageView向左移动5。
 *  imageEdgeInsets的right如果为-10,则button的imageView向右移动5。
 *  imageEdgeInsets的top如果为+10,则button的imageView向下移动5。
 *  imageEdgeInsets的top如果为-10,则button的imageView向上移动5。
 *  imageEdgeInsets的bottom如果为+10,则button的imageView向上移动5。
 *  imageEdgeInsets的bottom如果为-10,则button的imageView向下移动5。
 */

#import "UIButton+JMImagePosition.h"

@implementation UIButton (JMImagePosition)

- (void)jm_setImagePosition:(JMImagePosition)postion spacing:(CGFloat)spacing {
    dispatch_async(dispatch_get_main_queue(), ^{
      self.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0);
      self.titleEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0);

        //CGFloat imageWith = self.imageView.image.size.width;
        //CGFloat imageHeight = self.imageView.image.size.height;
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
      CGFloat labelWidth = [self.titleLabel.text sizeWithFont:self.titleLabel.font].width;
        //CGFloat labelHeight = [self.titleLabel.text sizeWithFont:self.titleLabel.font].height;
#pragma clang diagnostic pop

      float imageOffsetX = 0;
      float imageOffsetY = 0;
      float labelOffsetX = 0;
      float labelOffsetY = 0;

      /**
       // image中心移动的x距离
       CGFloat imageOffsetX = (imageWith + labelWidth) / 2 - imageWith / 2;
       // image中心移动的y距离
       CGFloat imageOffsetY = imageHeight / 2 + spacing / 2;
       // label中心移动的x距离
       CGFloat labelOffsetX = (imageWith + labelWidth / 2) - (imageWith + labelWidth) / 2;
       // label中心移动的y距离
       CGFloat labelOffsetY = labelHeight / 2 + spacing / 2;
       */

      switch (postion) {
          case LXMImagePositionLeft: {
              float selfWidth = self.bounds.size.width;

              float imageCenterX = self.imageView.center.x;
              float imageWidth = self.imageView.frame.size.width;

              float labelCenterX = self.titleLabel.center.x;

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
              float labelWidth = [self.titleLabel.text sizeWithFont:self.titleLabel.font].width;
#pragma clang diagnostic pop

              //左边边距:(button的总高度-imageView的高度-label的高度-间隔的高度)/2
              float leftPadding = (selfWidth - imageWidth - labelWidth - spacing) / 2;

              imageOffsetX = leftPadding + imageWidth / 2 - imageCenterX;
              labelOffsetX = leftPadding + imageWidth + spacing + labelWidth / 2 - labelCenterX;

              self.imageEdgeInsets = UIEdgeInsetsMake(0, imageOffsetX, 0, -imageOffsetX);
              self.titleEdgeInsets = UIEdgeInsetsMake(0, labelOffsetX - labelWidth, 0, -labelOffsetX - labelWidth);
          } break;

          case LXMImagePositionRight: {
              float selfWidth = self.bounds.size.width;

              float imageCenterX = self.imageView.center.x;
              float imageWidth = self.imageView.frame.size.width;

              float labelCenterX = self.titleLabel.center.x;

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
              float labelWidth = [self.titleLabel.text sizeWithFont:self.titleLabel.font].width;
#pragma clang diagnostic pop

              //左边边距:(button的总高度-imageView的高度-label的高度-间隔的高度)/2
              float leftPadding = (selfWidth - imageWidth - labelWidth - spacing) / 2;

              labelOffsetX = leftPadding + labelWidth / 2 - labelCenterX;
              imageOffsetX = leftPadding + labelWidth + spacing + imageWidth / 2 - imageCenterX;

              self.imageEdgeInsets = UIEdgeInsetsMake(0, imageOffsetX, 0, -imageOffsetX);
              self.titleEdgeInsets = UIEdgeInsetsMake(0, labelOffsetX - labelWidth, 0, -labelOffsetX - labelWidth);
          } break;

          case LXMImagePositionTop: {
              self.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0);
              self.titleEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0);

              float selfCenterX = self.bounds.size.width / 2.0;
              float selfHeight = self.bounds.size.height;

              float imageCenterX = self.imageView.center.x;
              float imageCenterY = self.imageView.center.y;
              float imageHeight = self.imageView.frame.size.height;

              float labelCenterX = self.titleLabel.center.x;
              float labelCenterY = self.titleLabel.center.y;
              float labelHeight = self.titleLabel.frame.size.height;

              //顶部边距:(button的总高度-imageView的高度-label的高度-间隔的高度)/2
              float topPadding = (selfHeight - imageHeight - labelHeight - spacing) / 2;

              //表示图片水平移动的距离,而不是图片的坐标,因此selfCenterX - imageCenterX表示移动这么多距离才能到中心点.
              imageOffsetX = selfCenterX - imageCenterX;

              /**
               当图片和文本长度加起来比按钮自身宽度还宽时,imageCenterX就会固定不变
               当图片和文本加起来按钮自身宽度小的时候,图片的宽度和文本的宽度加起来后是居中显示的,但文本过长时就会变成...,因此imageCenterX就不准确了.但是图片最多只会移到按钮的最左边.
               */
              if (self.imageView.frame.size.width + labelWidth > self.bounds.size.width) {
                  imageOffsetX = selfCenterX - self.imageView.frame.size.width / 2;
              }

              //NSLog(@"self.imageView.frame.size.width = %f", self.imageView.frame.size.width);
              //NSLog(@"labelWidth = %f", labelWidth);
              //NSLog(@"self.bounds.size.width = %f", self.bounds.size.width);
              //NSLog(@"self.imageView.frame = %@", NSStringFromCGRect(self.imageView.frame));
              //NSLog(@"self.titleLabel.frame = %@", NSStringFromCGRect(self.titleLabel.frame));
              //NSLog(@"self.bounds = %@", NSStringFromCGRect(self.bounds));

              //表示图片垂直移动的距离,而不是图片的坐标,topPadding+imageHeight/2为目标位置
              imageOffsetY = topPadding + imageHeight / 2 - imageCenterY;

              labelOffsetX = selfCenterX - labelCenterX;
              labelOffsetY = topPadding + imageHeight + spacing + labelHeight / 2 - labelCenterY;

              self.imageEdgeInsets = UIEdgeInsetsMake(imageOffsetY, imageOffsetX, -imageOffsetY, -imageOffsetX);
              self.titleEdgeInsets = UIEdgeInsetsMake(labelOffsetY, labelOffsetX - labelWidth, -labelOffsetY, -labelOffsetX - labelWidth);
          } break;

          case LXMImagePositionBottom: {
              float selfCenterX = self.bounds.size.width / 2.0;
              float selfHeight = self.bounds.size.height;

              float imageCenterX = self.imageView.center.x;
              //float imageCenterY = self.imageView.center.y;
              float imageHeight = self.imageView.frame.size.height;

              float labelCenterX = self.titleLabel.center.x;
              float labelCenterY = self.titleLabel.center.y;
              float labelHeight = self.titleLabel.frame.size.height;

              //顶部边距:(button的总高度-imageView的高度-label的高度-间隔的高度)/2
              float topPadding = (selfHeight - imageHeight - labelHeight - spacing) / 2;

              //表示图片水平移动的距离,而不是图片的坐标,因此selfCenterX - labelCenterX表示移动这么多距离才能到中心点.
              labelOffsetX = selfCenterX - labelCenterX;
              //表示图片垂直移动的距离,而不是图片的坐标,topPadding+labelHeight/2为目标位置
              labelOffsetY = topPadding + labelHeight / 2 - labelCenterY;

              imageOffsetX = selfCenterX - imageCenterX;
              imageOffsetY = topPadding + labelHeight + spacing + imageHeight / 2 - labelCenterY;

              self.imageEdgeInsets = UIEdgeInsetsMake(imageOffsetY, imageOffsetX, -imageOffsetY, -imageOffsetX);
              self.titleEdgeInsets = UIEdgeInsetsMake(labelOffsetY, labelOffsetX - labelWidth, -labelOffsetY, -labelOffsetX - labelWidth);
          } break;

          default:
              break;
      }
    });
}

@end
